/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.billingAndPaymentProcessing.entity;

import crm.clientManagement.entity.AddressEntity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import crm.salesOrderProcessing.entity.DeliveryOrderEntity;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.persistence.OneToMany;

/**
 *
 * @author xhx
 */
@Entity
public class InvoiceEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private double amountPayable;
    private double payment_balance;
    private String invoiceType;
    private double amountPaid =0;
    private String notes;
    private boolean abolished = false;
    private boolean isPaid = false;
    private Long createdBy;
    
    
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date issuing_date;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dueDate;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date paymentDate;
    
    @OneToOne(cascade={CascadeType.MERGE})
    private AddressEntity ourAdrress;
    
    @OneToMany(cascade={CascadeType.ALL},targetEntity=DunningEmailEntity.class, mappedBy="invoice")
    private List<DunningEmailEntity> dunningEmails;
    
    @OneToOne
    private DeliveryOrderEntity deliveryOrder;
    
    @OneToOne(cascade={CascadeType.ALL},mappedBy="invoice")
    private ReceiptEntity receipt;
 
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof InvoiceEntity)) {
            return false;
        }
        InvoiceEntity other = (InvoiceEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "finance.billingAndPayment.entity.InvoiceEntity[ id=" + id + " ]";
    }

    public double getPayment_balance() {
        return payment_balance;
    }

    public void setPayment_balance() {
        payment_balance = amountPayable - amountPaid;
    }

    public String getInvoiceType() {
        return invoiceType;
    }

    public void setInvoiceType(String invoiceType) {
        this.invoiceType = invoiceType;
    }

    public Date getIssuing_date() {
        return issuing_date;
    }

    public void setIssuing_date(Date issuing_date) {
        this.issuing_date = issuing_date;
    }

    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public Date getPaymentDate() {
        return paymentDate;
    }

    public void setPaymentDate(Date paymentDate) {
        this.paymentDate = paymentDate;
    }

    public DeliveryOrderEntity getDeliveryOrder() {
        return deliveryOrder;
    }

    public void setDeliveryOrder(DeliveryOrderEntity deliveryOrder) {
        this.deliveryOrder = deliveryOrder;
    }

    public double getAmountPayable() {
        return amountPayable;
    }

    public void setAmountPayable(double amountPayable) {
        this.amountPayable = amountPayable;
    }

    public double getAmountPaid() {
        return amountPaid;
    }

    public void setAmountPaid(double amountPaid) {
        this.amountPaid = amountPaid;
    }
    public void addAmountPaid(double paymentAmount){
       amountPaid = amountPaid + paymentAmount;
    }

    public AddressEntity getOurAdrress() {
        return ourAdrress;
    }

    public void setOurAdrress(AddressEntity ourAdrress) {
        this.ourAdrress = ourAdrress;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public boolean isAbolished() {
        return abolished;
    }

    public void setAbolished(boolean abolished) {
        this.abolished = abolished;
    }

    public ReceiptEntity getReceipt() {
        return receipt;
    }

    public void setReceipt(ReceiptEntity receipt) {
        this.receipt = receipt;
    }

    public boolean isIsPaid() {
        return isPaid;
    }

    public void setIsPaid(boolean isPaid) {
        this.isPaid = isPaid;
    }

    public Long getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
    }

    public List<DunningEmailEntity> getDunningEmails() {
        return dunningEmails;
    }

    public void setDunningEmails(List<DunningEmailEntity> dunningEmails) {
        this.dunningEmails = dunningEmails;
    }

    public String getIssueDate() {
        String dateForReport;
        DateFormat formatter;
        formatter = new SimpleDateFormat("dd-MMM-yy");
        dateForReport = formatter.format(this.issuing_date);

        return dateForReport;
    }
    
    public String getPaymentDay() {
        String dateForReport;
        DateFormat formatter;
        formatter = new SimpleDateFormat("dd-MMM-yy");
        dateForReport = formatter.format(this.paymentDate);

        return dateForReport;
    }

    
}
